// 接口地址 post 请求/users-info/login
//参数 {"password": "","username": ""}
//ajax获取表单输入数据，去登录登录成功后隐藏.login-box
$(document).ready(function () {
    var a = localStorage.getItem("token");
    if(a != null){
        $(".login-box").hide();
    }
    //把昵称获取到放在页面
    var nickname = localStorage.getItem("nickname");
    $("#username").text(nickname);

    $("#submit").click(function () {
        var username = $("input[name='username']").val();
        var password = $("input[name='password']").val();
        let data = {"password": password, "username": username};
        $.ajax({
            type: "post",
            url: "/users-info/login",
            data: JSON.stringify(data),
            contentType: "application/json",
            success: function (data) {//成功的响应
                if(data.code == 86203){
                    alert(data.msg);
                }
                var token = data.data.token;
                //localStorage 保存token
                localStorage.setItem("token", token);
                localStorage.setItem("nickname", data.data.nickname);
                localStorage.setItem("userId", data.data.userId);
                $("#username").text(data.data.nickname);
                $(".login-box").hide();
                //去触发长连接
                loginSuccess();
            },
            error: function (data) {//失败的响应
                //从异常拿数据
                var errorList = data.responseJSON.data;
                $("#error-msg").html("");
                //errorList用p标签包裹起来
                for (var i = 0; i < errorList.length; i++) {
                    $("#error-msg").append("<p>" + errorList[i] + "</p>");
                }
                console.log(data);
            }
        });
    });
});

